package com.vr.cloudpicture.controller;

import cn.hutool.core.bean.BeanUtil;
import com.vr.cloudpicture.annotation.AuthCheck;
import com.vr.cloudpicture.common.BaseResponse;
import com.vr.cloudpicture.common.PageResult;
import com.vr.cloudpicture.common.ResultUtils;
import com.vr.cloudpicture.constant.UserConstant;
import com.vr.cloudpicture.enums.SpaceLevelEnum;
import com.vr.cloudpicture.exception.ErrorCode;
import com.vr.cloudpicture.exception.ThrowUtils;
import com.vr.cloudpicture.manager.auth.SpaceUserAuthManager;
import com.vr.cloudpicture.manager.auth.annotation.SaSpaceCheckPermission;
import com.vr.cloudpicture.manager.auth.model.SpaceUserPermissionConstant;
import com.vr.cloudpicture.model.dto.DeleteRequest;
import com.vr.cloudpicture.model.dto.space.SpaceAddRequest;
import com.vr.cloudpicture.model.dto.space.SpaceEditRequest;
import com.vr.cloudpicture.model.dto.space.SpaceQueryRequest;
import com.vr.cloudpicture.model.dto.space.SpaceUpdateRequest;
import com.vr.cloudpicture.model.entity.Space;
import com.vr.cloudpicture.model.vo.SpaceLevelVO;
import com.vr.cloudpicture.model.vo.SpaceVO;
import com.vr.cloudpicture.model.dto.user.UserLogin;
import com.vr.cloudpicture.service.ISpaceService;
import com.vr.cloudpicture.service.IUserService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 空间控制器
 *
 * @author hzh
 * @date 2025/05/18
 */
@Slf4j
@RestController()
@RequestMapping("/space")
@RequiredArgsConstructor
public class SpaceController {
    private final ISpaceService spaceService;
    private final IUserService userService;
    private final SpaceUserAuthManager spaceUserAuthManager;


    @ApiOperation(value = "通过id获取空间,管理员用的")
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.USER_ROLE_ADMIN)
    public BaseResponse<Space> getSpaceById(Long id) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR);
        Space space = spaceService.getById(id);
        ThrowUtils.throwIf(space == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(space);
    }

    @ApiOperation(value = "通过id获取空间,创建者本人或者管理员可用")
    @GetMapping("/get/vo")
    @SaSpaceCheckPermission(SpaceUserPermissionConstant.PICTURE_VIEW)
    public BaseResponse<SpaceVO> getSpaceVOById(Long id, HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(loginUser==null, ErrorCode.NOT_LOGIN_ERROR);
        Space space = spaceService.getById(id);
        // ThrowUtils.throwIf(!userService.isAdmin(loginUser) && !space.getUserId().equals(loginUser.getId()),  ErrorCode.NOT_AUTH_ERROR);
        SpaceVO  spaceVO = BeanUtil.toBean(space, SpaceVO.class);
        spaceVO.setPermissions(spaceUserAuthManager.getPermissionList(space,loginUser));
        return ResultUtils.success(spaceVO);
    }

    @ApiOperation(value = "获取空间列表,仅管理员可见")
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.USER_ROLE_ADMIN)
    public BaseResponse<PageResult<Space>> listSpaceByPage(@RequestBody SpaceQueryRequest spaceQueryRequest) {
        return ResultUtils.success(spaceService.listSpaceByPage(spaceQueryRequest));
    }
    @ApiOperation(value = "获取空间列表")
    @PostMapping("/list/page/vo")
    public BaseResponse<PageResult<SpaceVO>> listSpaceVOByPage(@RequestBody SpaceQueryRequest spaceQueryRequest,HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        return ResultUtils.success(spaceService.listSpaceVOByPage(spaceQueryRequest,loginUser));
    }


    @ApiOperation(value = "更新空间")
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.USER_ROLE_ADMIN)
    public BaseResponse<Boolean> updateSpace(@RequestBody SpaceUpdateRequest spaceUpdateRequest, HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        return ResultUtils.success(spaceService.updateSpace(spaceUpdateRequest, loginUser));
    }

    @ApiOperation(value = "更新空间")
    @PostMapping("/edit")
    public BaseResponse<Boolean> editSpace(@RequestBody SpaceEditRequest spaceEditRequest, HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser==null,ErrorCode.NOT_LOGIN_ERROR);
        return ResultUtils.success(spaceService.editSpace(spaceEditRequest,loginUser));
    }

    @ApiOperation(value = "创建空间")
    @PostMapping("/add")
    public BaseResponse<Long> addSpace(@RequestBody SpaceAddRequest spaceAddRequest, HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        ThrowUtils.throwIf(spaceAddRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(spaceService.addSpace(spaceAddRequest, loginUser));
    }
    @ApiOperation(value = "删除空间")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteSpace(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        return ResultUtils.success(spaceService.deleteSpace(deleteRequest, loginUser));
    }
    @ApiOperation(value = "获取空间等级列表")
    @GetMapping("/list/level")
    public BaseResponse<List<SpaceLevelVO>> listSpaceLevel() {
        return ResultUtils.success(Arrays.stream(SpaceLevelEnum.values())
                .map(spaceLevelEnum -> SpaceLevelVO.builder()
                        .value(spaceLevelEnum.getValue())
                        .text(spaceLevelEnum.getText())
                        .maxCount(spaceLevelEnum.getMaxCount())
                        .maxSize(spaceLevelEnum.getMaxSize())
                        .build()).collect(Collectors.toList()));
    }
}
